#include "PatientDelegate.h"

#include <QPainter>
#include <QApplication>
#include <QMouseEvent>
#include<QAbstractItemView>

namespace UserManagementSystem {

PatientDelegate::PatientDelegate(QObject *parent)
    :QStyledItemDelegate(parent)
{

    //加载图标资源
    LoadIcon();
}

void PatientDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{

    //保存当前画笔设置
    painter->save();

    //取到person的数据
    PatientData person=index.data(Qt::DisplayRole).value<PatientData>();


    //取得绘制的矩形框
    QRect rect=option.rect;

    //给取到的矩形框绘制背景色
    painter->fillRect(rect,option.state & QStyle::State_Selected ? QColor("#EEF4FF"):Qt::white);

    //判断悬浮状态

    if(option.state& QStyle::State_MouseOver){

        //给rect区域绘制半透明背景
        QColor blue(238,244,255,255);
        painter->setBrush(blue);
        painter->setPen(Qt::NoPen);
        painter->drawRoundedRect(rect,16,16);

        //绘制不透明蓝色竖线，在左边界上
        int center_y=rect.top()+(rect.height()/2);//

        //蓝线的顶端和末端高度
        int line_top=center_y-20;
        int line_bottom=center_y+20;//上下各30

        QPoint line_start(rect.left(),line_top);//蓝色框左上的点
        QPoint line_end(rect.left(),line_bottom);//蓝色框左下的点

        QPen pen(QColor(89,128,202));
        pen.setWidth(4);
        painter->setPen(pen);

        painter->drawLine(line_start,line_end);

        painter->setPen(QColor(1,1,1));
    }


    //绘制头像,根据 男女绘制头像,o=男，1=女,头像绘制成30*30的
    int width=(rect.height()-20);
    QRect profile_recct(rect.left()+10,rect.top()+10,width,width);
    if(person.gender==0){
        //绘制男头像
        painter->drawPixmap(profile_recct,m_profile_man);

    }else if(person.gender==1){
        //绘制女头像
        painter->drawPixmap(profile_recct,m_profile_female);
    }else if(person.gender==2){
        //其他性别头像
        painter->drawPixmap(profile_recct,m_profile_other);

    }


    //名称，时间,根据头像的相对位置确定
    QRect text_name(profile_recct.right()+10,rect.top()+10,126,22);//宽150，高20
    QRect text_create_time(profile_recct.right()+10,text_name.bottom()+10,134,20);//宽150，高20
    painter->drawText(text_name,person.name);
    painter->drawText(text_create_time,person.create_time);

    //年龄
    QRect age(rect.right()-30,rect.top()+20,50,20);
    painter->drawText(age,QString("年龄: %1").arg(person.age));

    //下方两个图标按钮,按钮都设成10*10de
    QRect edit_rect(rect.right()-100,age.bottom()+10,20,20);
    QRect delete_rect(edit_rect.right()+10,edit_rect.top(),20,20);

    //绘制两个按钮的图标
    int state=m_buttonsStateMap[index];
    QPixmap edit_pix=(state==1)?edit_clicked:edit_default;
    QPixmap delete_pix=(state==2)?delete_clicked:delete_default;


    //画出两个按钮的范围
    painter->drawRect(edit_rect);
    painter->drawRect(delete_rect);

    painter->drawPixmap(edit_rect,edit_pix);
    painter->drawPixmap(delete_rect,delete_pix);

    painter->restore();
}

bool PatientDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    //捕获鼠标事件：鼠标移动事件，鼠标点击事件

    const QAbstractItemView* view=qobject_cast<const QAbstractItemView *>(option.widget);
    int viewport_widget=0;
      if(view){
        viewport_widget=view->viewport()->width();
      }

    //取得绘制的矩形框
    QRect rect=option.rect;
    rect.setWidth(viewport_widget);



    QRect btn_1(rect.right()-100,rect.top()+50,20,20);
    QRect btn_2(btn_1.right()+10,btn_1.top(),20,20);




    if(event->type()==QEvent::MouseMove){
        QMouseEvent* my_event=static_cast<QMouseEvent*>(event);

        //判断鼠标的位置在不在这两个鼠标框内
        if(btn_1.contains(my_event->pos())){
            m_buttonsStateMap[index]=1;//代表点击了编辑按钮
        }else if(btn_2.contains(my_event->pos())){
            m_buttonsStateMap[index]=2;//代表点击了删除按钮
        }else{
            m_buttonsStateMap[index]=0;//代表点击了删除按钮
            return true;
        }
    }

    if(event->type()==QEvent::MouseButtonRelease){


        QMouseEvent* my_event=static_cast<QMouseEvent*>(event);
        // qDebug()<<"****"<<rect<<","<<btn_1<<","<<btn_2;
        // qDebug()<<my_event->pos()<<"****";
        if(btn_1.contains(my_event->pos())){
            m_buttonsStateMap[index]=1;//代表点击了编辑按钮
            emit On_EditPersonData_Btn(index.row());//触发边界信号
            return true;
        }
        if(btn_2.contains(my_event->pos())){
            m_buttonsStateMap[index]=2;//代表点击了删除按钮
            emit On_DeletePerson_Btn(index.row());
            return true;
        }
    }
    return false;

}

void PatientDelegate::LoadIcon()
{

    QString man_path=QApplication::applicationDirPath()+"/res/PatientManager/man.png";
    QString female_path=QApplication::applicationDirPath()+"/res/PatientManager/female.png";
    QString other_path=QApplication::applicationDirPath()+"/res/PatientManager/other.png";


    QString delete_path=QApplication::applicationDirPath()+"/res/PatientManager/delete_person.png";
    QString delete_clicked_path=QApplication::applicationDirPath()+"/res/PatientManager/delete_person_hover.png";

    QString edit_path=QApplication::applicationDirPath()+"/res/PatientManager/edit_person.png";
    QString edit_clicked_path=QApplication::applicationDirPath()+"/res/PatientManager/edit_person_hover.png";

    m_profile_man.load(man_path);
    m_profile_female.load(female_path);
    m_profile_other.load(other_path);


    delete_default.load(delete_path);
    delete_clicked.load(delete_clicked_path);
    edit_default.load(edit_path);
    edit_clicked.load(edit_clicked_path);
}

}
